const {User} = require('../../../models/User');
var bcrypt = require('bcryptjs');

module.exports = async (req, res) => {
	const {user_id, oldpwd, newpwd} = req.body;
    try{
        const user = await User.findOne({user_id:user_id});
        let isValid =  bcrypt.compareSync(oldpwd, user.password);
        console.log(oldpwd,newpwd)
        if(isValid){
            const salt = bcrypt.genSaltSync(10);
            const pass = bcrypt.hashSync(newpwd, salt);
            const updatePwd = User.updateOne({user_id:user_id},{$set:{"password":pass}});
            res.send({errCode:"0",msg:"密码修改成功"})
        }else{
            res.status(400).send({errorCode:"1",msg:"原密码错误"});
        }
    }catch(e){
        console.log(e)
        res.status(400).send({errorCode:"1",msg:"修改失败"});
    }
	
}